<?php

namespace app\admin\controller;


use think\Controller;
use think\Db;
use think\Request;
use think\Response;
use think\view;


class User extends Admincontroller
{
    public function index()
    {
        return view('admin@user/tables');
    }

    public function userlist()
    {


        $data=Db::table('user')
            ->field(['id','username','name'])
            ->select();
        foreach($data as $v){
//            var_dump($v);die;
            $rid=Db::table('user_role')
                ->field('rid')
                ->where('uid',$v['id'])
                ->select();
//           var_dump($rid);die;
            //定义空数组
            $roles = array();
            //遍历
            foreach($rid as $value){
                $roles[] =Db::table('role')
                    ->field('name')
                    ->find($value);
            }
            $v['roles'] = $roles; //将新得到角色信息放置到$v中
//            var_dump($v['roles']);die;
            $arr[] = $v;
        }
//        var_dump($arr);die;

        return view('user/userlist',['data'=>$arr]);
    }
    public function usercreate()
    {
        return view('user/usercreate');
    }
    public function save(Request $request)
    {
//        dump($_POST);die;
        $d=$request->post();
        $info['username']=$d['username'];
        $info['userpass']=hash('md5',$d['userpass']);
        $info['name']=$d['name'];
//        var_dump($info['userpass']);
//        var_dump($info);die();
        $result=Db::table('user')
                    ->insertGetId($info);
        if ($result>0){
            $this->success('添加成功','admin/User/userlist');
        }else{
            $this->error("添加失败");
        }
        var_dump($result);die;
    }

    public function rolelist($id)
    {
        $rid=Db::table('user_role')
                ->field(['rid'])
                ->where(['uid'=>$id])
                ->select($id);
//        var_dump($rid);die;
        $roles=array();
        foreach ($rid as $v){
            $role=Db::table('role')
                    ->field(['name'])
                    ->where(['id'=>$v['rid']])
                    ->find();
//            var_dump($role);die;
            $roles[]=$role['name'];

        }
//        var_dump($roles);die;
//        var_dump($roles);die;
        $res=Db::table('role')
                    ->field(['name','id'])
                    ->select();
//        var_dump($res);die;
        return view('user/rolelist',['list'=>$res,'roles'=>$roles,'id'=>$id]);


    }

    public function dorolelist(Request $request)
    {

        $id=$request->post();
        //判读必须选择一个角色
//        var_dump($id);
        if(empty($id['roles'])){
            $this->error("请选择一个角色！");
        }

//            var_dump($id);die;
        //清除用户所有的角色信息，避免重复添加
            $del=Db::table('user_role')
                    ->where(['uid'=>$id['uid']])
                    ->delete();
//            var_dump($del);die;
            foreach($id['roles'] as $v){
//                var_dump($v);die;
                $data['uid']=$id['uid'];
                $data['rid']=$v;
//                var_dump($data);die;
                $res=Db::table('user_role')
                    ->insertGetId($data);
//                var_dump($res);die;
            }

                $this->success('添加成功','admin/User/userlist');
//            var_dump($result);
    }

    public function userdel($id)
    {
//        var_dump($id);
          $res=Db::table('user')
                ->where(['id'=>$id])
                ->delete();
          $res1=Db::table('user_role')
            ->where(['uid'=>$id])
            ->delete();
//          var_dump($res1);die;
        if (!empty($res)&&!empty($res)){
            $this->success('删除成功','admin/User/userlist');
        }else{
            $this->error('删除失败');
        }
    }

    public function useredit($id)
    {
        $res=Db::table('user')
                ->field(['name','username'])
                ->where(['id'=>$id])
                ->find();
//        var_dump($res);
        return view('user/useredit',['id'=>$id,'list'=>$res]);
    }

    public function douseredit($id)
    {
//        var_dump($_POST);
        $data=$_POST;
        $res=Db::table('user')
            ->where(['id'=>$id])
            ->update($data);
//        var_dump($res);
        if ($res){
            $this->success('添加成功','admin/User/userlist');
        }else{
            $this->error('添加失败');
        }

    }



}